package com.cardone.common.io;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.*;
import lombok.Setter;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * Created by Administrator on 2014/12/3.
 */
public class OssManager extends AbstractFsManager implements FsManager {
    @Setter
    private OSSClient ossClient;

    @Setter
    private String bucketName;

    /**
     * 键前缀名称
     */
    private String keyPrefix = "entityFile/";

    public OssManager() {
        super();
    }

    private String newKey(String key) {
        if (StringUtils.isBlank(keyPrefix)) {

            return key;
        }

        return keyPrefix + key;
    }

    @Override
    public void saveFile(InputStream inputStream, long size, String filename, String contentType) throws IOException {
        ObjectMetadata objectMeta = new ObjectMetadata();

        if(StringUtils.isNotBlank(contentType)){
            objectMeta.setContentType(contentType);
        }

        objectMeta.setContentLength(size);

        String newKey = this.newKey(filename);

        ossClient.putObject(bucketName, newKey, inputStream, objectMeta);
    }

    @Override
    public void writeTo(OutputStream toOutputStream, String filename) throws IOException {
        String newKey = this.newKey(filename);

        OSSObject ossObject = ossClient.getObject(new GetObjectRequest(bucketName, newKey));

        if (ossObject == null) {
            return;
        }

        try (InputStream is = ossObject.getObjectContent()) {
            IOUtils.copy(is, toOutputStream);
        }
    }

    @Override
    public void saveFile(InputStream inputStream, long size, String filename) throws IOException {
        saveFile(inputStream, size, filename, null);
    }

    @Override
    public void writeTo(HttpServletResponse response, String filename) throws IOException {
        String newKey = this.newKey(filename);

        OSSObject ossObject = ossClient.getObject(new GetObjectRequest(bucketName, newKey));

        if (ossObject == null) {
            return;
        }

        response.setDateHeader("Expires", 0);
        response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
        response.setHeader("Pragma", "no-cache");

        final String fileExtension = FilenameUtils.getExtension(filename);

        for (final String writeToKey : writeToMap.keySet()) {
            if (StringUtils.containsIgnoreCase(writeToKey, fileExtension)) {
                writeToMap.get(writeToKey).run(response, filename);

                try (OutputStream out = response.getOutputStream()) {
                    try (InputStream is = ossObject.getObjectContent()) {
                        IOUtils.copy(is, out);
                    }

                    out.flush();

                    response.flushBuffer();
                }

                return;
            }
        }

        try (OutputStream out = response.getOutputStream()) {
            try (InputStream is = ossObject.getObjectContent()) {
                IOUtils.copy(is, out);
            }

            out.flush();

            response.flushBuffer();
        }
    }

    @Override
    public void writeTo(String toFilename, String filename) throws IOException {
        writeTo(new File(toFilename), filename);
    }

    @Override
    public void writeTo(File toFile, String filename) throws IOException {
        String newKey = this.newKey(filename);

        ossClient.getObject(new GetObjectRequest(bucketName, newKey), toFile);
    }
}
